package com.hoperun.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AdminOrganizations;
import com.hoperun.bean.AdminOrganizationsManager;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminOrganizationsManagerSV;
import com.hoperun.utils.LoginHelper;

/**
 * Copyright: Copyright (c) 2016 GUO~AN <br/>
 * ClassName: AdminOrganizationsManagerController.java
 * 
 * @Description:
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2016-1-8 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2016-1-8
 *        fuyu v1.0.0 修改理由
 */
@Controller
@RequestMapping("/admin/admin/organizations/manager")
@RequiresUser
public class AdminOrganizationsManagerController extends BaseController {

	@Resource
	private IAdminOrganizationsManagerSV adminOrganizationsManagerSV;

	@ModelAttribute
	public void populateModel(final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/create")
	public String create(String mids, ModelMap modelMap,
			final HttpServletRequest request) {
		try {

			modelMap.put("mids", mids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "adminAlone:adminmanager.org";
	}

	@RequestMapping("/tree")
	@ResponseBody
	public DataView organizationTree(
			@RequestParam(value = "orgId", defaultValue = "0") Long orgId) {
		DataView dataView = new DataView();
		try {

			Long managerId = LoginHelper.getLoginUserId();

			List<AdminOrganizations> list = adminOrganizationsManagerSV
					.queryManagerMaxOrganization(managerId, orgId);

			if (CollectionUtils.isNotEmpty(list)) {
				dataView.setCode(Constant.SUCCESS);
				dataView.setData(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/bind")
	@ResponseBody
	public DataView bind(@RequestParam(value = "oids") String oids,
			@RequestParam(value = "mids") String mids) {
		DataView dataView = new DataView();
		try {

			List<AdminOrganizationsManager> list = adminOrganizationsManagerSV
					.createBind(oids, mids);

			if (CollectionUtils.isNotEmpty(list)) {
				dataView.setCode(Constant.SUCCESS);
				dataView.setMessage("绑定完成");
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new XHZException(e.getMessage());
		}
		return dataView;
	}

	@RequestMapping("/unbind")
	@ResponseBody
	public DataView unbind(@RequestParam(value = "mids") String mids) {
		DataView dataView = new DataView();
		try {

			adminOrganizationsManagerSV.deleteBindManager(mids);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("已经成功解除部门信息");

		} catch (Exception e) {
			e.printStackTrace();
			throw new XHZException(e.getMessage());
		}
		return dataView;
	}
}
